//
//  NRWSubscribeVC.m
//  NRWMall
//
//  Created by 邢毅 on 2018/6/19.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWSubscribeVC.h"

#import "NRWBrandSeriesVC.h"

#import "NRWBuyCarFilterHeader.h"
#import "NRWSliderCollectionCell.h"
#import "NRWFilterColorCell.h"
#import "NRWPriceChooseCell.h"
#import "NRWBuyCarFilterManager.h"
#import "NRWTabelViewCollectionCell.h"
#import "NRWCooperateCityPickView.h"

#import "NRWBuyCarConditionModel.h"
#import "NRWBuyCarFilterOptionsModel.h"
#import "NRWBrightConfigModel.h"
#import "NRWCooperationCityListModel.h"
#import "NRWBuyCarFilterHeaderModel.h"

#import "NRWGetFilterInfoRequest.h"
#import "NRWGetBrightSpotConfListAPI.h"
#import "NRWGetCooperationPositionApi.h"
#import "NRWInsertSubscribeInfoApi.h"
#import "NRWSearchSubcribeApi.h"

@interface NRWSubscribeVC ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, NRWSliderCollectionCellDelegate, NRWBrandSeriesVCDelegate, NRWCooperateCityPickViewDelegate, YTKChainRequestDelegate>

@property (strong, nonatomic) UICollectionView *collection;

@property (strong, nonatomic) NSArray<NSString *> *carMileageTitleArray;

@property (strong, nonatomic) NSArray<NSString *> *carAgeTitleArray;

@property (strong, nonatomic) NSArray<NRWBuyCarPriceModel *> *priceModelArray;

@property (strong, nonatomic) NSArray<NRWBuyCarFilterHeaderModel *> *sectionHeaderArray;

@property (strong, nonatomic) NSMutableArray<NRWBrightConfigItemModel *> *brightConfigSelectedArray;

@property (strong, nonatomic) NRWBuyCarConditionModel *conditionModel;

@property (strong, nonatomic) NRWBuyCarFilterOptionsModel *optionsModel;

@property (strong, nonatomic) NRWBrightConfigModel *configModel;

@property (strong, nonatomic) NRWGetFilterInfoRequest *infoApi;

@property (strong, nonatomic) NRWGetBrightSpotConfListAPI *brightConfigApi;

@property (strong, nonatomic) NRWGetCooperationPositionApi *positionApi;

@property (strong, nonatomic) NRWInsertSubscribeInfoApi *insertSubscribeInfoApi;

@property (strong, nonatomic) NRWSearchSubcribeApi *searchSubcribeApi;

@property (strong, nonatomic) NRWCooperateCityPickView *cityPickView;

@property (copy, nonatomic) NSString *templateId;

@property (strong, nonatomic) dispatch_group_t networkGroup;

@end

@implementation NRWSubscribeVC
{
    UIButton *_mResetBtn;
    UIButton *_mCompleteBtn;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.networkGroup = dispatch_group_create();
    self.navigationView.title = @"车辆订阅";
	
	[SVProgressHUD show];
    [self.infoApi start];
    [self.brightConfigApi start];
    dispatch_group_notify(self.networkGroup, dispatch_get_main_queue(), ^{
        [self.searchSubcribeApi start];
    });
    [self.positionApi start];
    
    [self setupUI];
    [self setupBottomButton];
    
    [self initData];
}

- (void)setupUI {
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;

    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, getNavHeight(), SCREEN_WIDTH(), SCREEN_HEIGHT() - getNavHeight() - getSafeBottomMargin() - 45) collectionViewLayout:layout];
    self.collection.backgroundColor = [UIColor whiteColor];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    [self.collection registerClass:NRWBuyCarFilterHeader.class forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self.collection registerClass:NRWSliderCollectionCell.class forCellWithReuseIdentifier:@"slider"];
    [self.collection registerClass:NRWFilterColorCell.class forCellWithReuseIdentifier:@"color"];
    [self.collection registerClass:NRWPriceChooseCell.class forCellWithReuseIdentifier:@"other"];
    [self.collection registerClass:NRWTabelViewCollectionCell.class forCellWithReuseIdentifier:@"tableCell"];
    
    [self.view addSubview:self.collection];
}

- (void)setupBottomButton {
    
    _mResetBtn = [[UIButton alloc] init];
    _mResetBtn.titleLabel.font = getThemeFont(17);
    _mResetBtn.frame = CGRectMake(0, self.collection.viewMaxY, SCREEN_WIDTH() / 2, 45);
    _mResetBtn.backgroundColor = getColor(@"#FFC74A");
    [_mResetBtn setTitle:@"重置" forState:UIControlStateNormal];
    [_mResetBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_mResetBtn addTarget:self action:@selector(resetBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_mResetBtn];
    
    _mCompleteBtn = [[UIButton alloc] init];
    _mCompleteBtn.titleLabel.font = getThemeFont(17);
    _mCompleteBtn.frame = CGRectMake(SCREEN_WIDTH() / 2, self.collection.viewMaxY, SCREEN_WIDTH() / 2, 45);
    _mCompleteBtn.backgroundColor = mainColor();
    [_mCompleteBtn setTitle:@"确认" forState:UIControlStateNormal];
    [_mCompleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_mCompleteBtn addTarget:self action:@selector(completeBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_mCompleteBtn];
}

- (void)initData {
    
    self.conditionModel.priceModel.priceTitle = @"不限";
//    self.conditionModel.gearbox = @"手动";
}

#pragma mark - TouchAction
- (void)resetBtnDidClick:(UIButton *)sender {
    
    self.conditionModel = [[NRWBuyCarConditionModel alloc] init];
    self.conditionModel.priceModel.priceTitle = @"不限";
//    self.conditionModel.gearbox = @"手动";
    self.sectionHeaderArray = [NRWBuyCarFilterManager getSubscribeTitleArray];
    [self.brightConfigSelectedArray removeAllObjects];
    
    [self.collection reloadData];
}

- (void)completeBtnDidClick:(UIButton *)sender {
    
    NSMutableArray *configNameArr = [NSMutableArray arrayWithCapacity:self.brightConfigSelectedArray.count];
    NSMutableArray *configIdArr = [NSMutableArray arrayWithCapacity:self.brightConfigSelectedArray.count];
    [self.brightConfigSelectedArray enumerateObjectsUsingBlock:^(NRWBrightConfigItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [configNameArr addObject:obj.confName];
        [configIdArr addObject:obj.confId];
    }];
    
    self.insertSubscribeInfoApi.param.templateCarAgeFrom = self.conditionModel.filterModel.carAgeFrom;
    self.insertSubscribeInfoApi.param.templateCarAgeTo = self.conditionModel.filterModel.carAgeTo;
    self.insertSubscribeInfoApi.param.templateCarTypeName = self.conditionModel.filterModel.carTypeName;
    self.insertSubscribeInfoApi.param.templateCarTypeId = self.conditionModel.filterModel.carTypeId;
    self.insertSubscribeInfoApi.param.templateCityId = self.conditionModel.cityModel.cityId;
    self.insertSubscribeInfoApi.param.templateCityName = self.conditionModel.cityModel.cityName;
    self.insertSubscribeInfoApi.param.templateConfigId = [configIdArr componentsJoinedByString:@","];
    self.insertSubscribeInfoApi.param.templateConfigName = [configNameArr componentsJoinedByString:@","];
    self.insertSubscribeInfoApi.param.templateEnviron = self.conditionModel.filterModel.carEnvironName;
    self.insertSubscribeInfoApi.param.templateGearbox = self.conditionModel.gearbox;
    self.insertSubscribeInfoApi.param.templateMileageFrom = self.conditionModel.filterModel.mileageFrom;
    self.insertSubscribeInfoApi.param.templateMileageTo = self.conditionModel.filterModel.mileageTo;
    self.insertSubscribeInfoApi.param.templateOutColor = self.conditionModel.filterModel.carOutColorName;
    self.insertSubscribeInfoApi.param.templatePriceFrom = self.conditionModel.priceModel.priceFrom;
    self.insertSubscribeInfoApi.param.templatePriceTo = self.conditionModel.priceModel.priceTo;
    self.insertSubscribeInfoApi.param.templateSeriesId = self.conditionModel.brandModel.seriesId;
    self.insertSubscribeInfoApi.param.templateSeriesName = self.conditionModel.brandModel.seriesName;
    self.insertSubscribeInfoApi.param.templateId = self.templateId;
    self.insertSubscribeInfoApi.param.userId = [NRWUserInfoModel shared].userId;
    
    if ([self.insertSubscribeInfoApi.param isPropertyNull]) {
        [NRWMessageHUD showMessage:@"订阅条件不能全部为空" forTime:1.5 withRemindId:remindWarning];
    } else {
		[SVProgressHUD show];
        [self.insertSubscribeInfoApi start];
    }
}

#pragma mark - network
- (void)requestFinished:(__kindof YTKBaseRequest *)request {
    
    if (request == self.infoApi) {
        
        dispatch_group_enter(self.networkGroup);
		if ([request.responseObject[@"code"] integerValue] == 0) {
			[NRWMessageHUD showMessage:request.responseObject[@"msg"] forTime:1.5 withRemindId:remindFail];
		} else {
			self.optionsModel = [NRWBuyCarFilterOptionsModel mj_objectWithKeyValues:request.responseObject[@"result"]];
			[self.collection reloadData];
		}
        dispatch_group_leave(self.networkGroup);
    } else if (request == self.brightConfigApi) {
		
		dispatch_group_enter(self.networkGroup);
		if ([request.responseObject[@"code"] integerValue] == 0) {
			[NRWMessageHUD showMessage:request.responseObject[@"msg"] forTime:1.5 withRemindId:remindFail];
		} else {
			self.configModel = [NRWBrightConfigModel mj_objectWithKeyValues:self.brightConfigApi.responseObject];
			[self.collection reloadData];
		}
        dispatch_group_leave(self.networkGroup);
    } else if (request == self.positionApi) {
		
		if ([request.responseObject[@"code"] integerValue] == 0) {
			[NRWMessageHUD showMessage:request.responseObject[@"msg"] forTime:1.5 withRemindId:remindFail];
		} else {
			self.cityPickView.cityModel = [NRWCooperationCityListModel mj_objectWithKeyValues:self.positionApi.responseObject];
		}
    } else if (request == self.insertSubscribeInfoApi) {
		
		[SVProgressHUD dismiss];
        if ([self.insertSubscribeInfoApi.responseObject[@"code"] integerValue] == 1) {
            [NRWMessageHUD showMessage:@"订阅成功" forTime:1.5 withRemindId:remindSuccess animationCompleted:^{
                [self.navigationController popViewControllerAnimated:YES];
            }];
        } else {
            [NRWMessageHUD showMessage:@"订阅失败" forTime:1.5 withRemindId:remindFail];
        }
    } else if (request == self.searchSubcribeApi) {
		
		[SVProgressHUD dismiss];
		if ([request.responseObject[@"code"] integerValue] == 0) {
			[NRWMessageHUD showMessage:request.responseObject[@"msg"] forTime:1.5 withRemindId:remindFail];
			return;
		}
		
        NRWSubscribeInfoModel *subscribeModel = [NRWSubscribeInfoModel mj_objectWithKeyValues:self.searchSubcribeApi.responseObject];
        self.templateId = subscribeModel.result.templateId;
        
        //两个滑块的title 临时数组
        NSArray *carAgeTitleArray = [NRWBuyCarFilterManager getBuyCarAgeTitleArray];
        NSArray *carMileageTitleArray = [NRWBuyCarFilterManager getBuyCarMileageTitleArray];
        
        //车龄相关信息赋值
        self.conditionModel.filterModel.carAgeFrom = subscribeModel.result.templateCarAgeFrom;
        self.conditionModel.filterModel.carAgeTo = subscribeModel.result.templateCarAgeTo;
        self.conditionModel.filterModel.carAgeTitle = subscribeModel.result.templateCarAgeTitle;
        if (isEmptyString(subscribeModel.result.templateCarAgeFrom) || subscribeModel.result.templateCarAgeFrom.integerValue == 0) {
            self.conditionModel.filterModel.carAgeFirstIndex = 0;
        } else {
            self.conditionModel.filterModel.carAgeFirstIndex = [carAgeTitleArray indexOfObject:subscribeModel.result.templateCarAgeFrom];
        }
        if (isEmptyString(subscribeModel.result.templateCarAgeTo) || subscribeModel.result.templateCarAgeTo.integerValue == 0) {
            self.conditionModel.filterModel.carAgeLastIndex = carAgeTitleArray.count - 1;
        } else {
            self.conditionModel.filterModel.carAgeLastIndex = [carAgeTitleArray indexOfObject:subscribeModel.result.templateCarAgeTo];
        }
        
        //里程相关信息赋值
        self.conditionModel.filterModel.mileageFrom = subscribeModel.result.templateMileageFrom;
        self.conditionModel.filterModel.mileageTo = subscribeModel.result.templateMileageTo;
        self.conditionModel.filterModel.mileageTitle = subscribeModel.result.templateMileageTitle;
        if (isEmptyString(subscribeModel.result.templateMileageFrom) || subscribeModel.result.templateMileageFrom.integerValue == 0) {
            self.conditionModel.filterModel.mileageFirstIndex = 0;
        } else {
            self.conditionModel.filterModel.mileageFirstIndex = [carMileageTitleArray indexOfObject:subscribeModel.result.templateMileageFrom];
        }
        if (isEmptyString(subscribeModel.result.templateMileageTo) || subscribeModel.result.templateMileageTo.integerValue == 0) {
            self.conditionModel.filterModel.mileageLastIndex = carMileageTitleArray.count - 1;
        } else {
            self.conditionModel.filterModel.mileageLastIndex = [carMileageTitleArray indexOfObject:subscribeModel.result.templateMileageTo];
        }
        
        //价格相关信息赋值
        self.conditionModel.priceModel.priceFrom = subscribeModel.result.templatePriceFrom;
        self.conditionModel.priceModel.priceTo = subscribeModel.result.templatePriceTo;
        self.conditionModel.priceModel.priceTitle = subscribeModel.result.templatePriceTitle;
        self.conditionModel.filterModel.carTypeName = subscribeModel.result.templateCarTypeName;
        self.conditionModel.filterModel.carTypeId = subscribeModel.result.templateCarTypeId;
        self.conditionModel.cityModel.cityName = subscribeModel.result.templateCityName;
        self.conditionModel.cityModel.cityId = subscribeModel.result.templateCityId;
        self.conditionModel.filterModel.carEnvironName = subscribeModel.result.templateEnviron;
        self.conditionModel.gearbox = subscribeModel.result.templateGearbox;
        self.conditionModel.filterModel.carOutColorName = subscribeModel.result.templateOutColor;
        self.conditionModel.brandModel.seriesId = subscribeModel.result.templateSeriesId;
        self.conditionModel.brandModel.seriesName = subscribeModel.result.templateSeriesName;
        
        NSArray *configNameArr = [subscribeModel.result.templateConfigName componentsSeparatedByString:@","];
        [self.configModel.result enumerateObjectsUsingBlock:^(NRWBrightConfigItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([configNameArr containsObject:obj.confName]) {
                [self.brightConfigSelectedArray addObject:obj];
            }
        }];
        
        self.sectionHeaderArray[0].detail = isEmptyString(self.conditionModel.priceModel.priceTitle)? @"不限": self.conditionModel.priceModel.priceTitle;
        self.sectionHeaderArray[1].detail = isEmptyString(self.conditionModel.filterModel.carAgeTitle)? @"不限": self.conditionModel.filterModel.carAgeTitle;
        self.sectionHeaderArray[2].detail = isEmptyString(self.conditionModel.filterModel.mileageTitle)? @"不限": self.conditionModel.filterModel.mileageTitle;
        self.sectionHeaderArray[3].detail = self.conditionModel.gearbox;
        self.sectionHeaderArray[4].detail = isEmptyString(self.conditionModel.filterModel.carOutColorName)? @"不限": self.conditionModel.filterModel.carOutColorName;
        self.sectionHeaderArray[5].detail = isEmptyString(self.conditionModel.filterModel.carTypeName)? @"不限": self.conditionModel.filterModel.carTypeName;
        self.sectionHeaderArray[6].detail = isEmptyString(self.conditionModel.filterModel.carEnvironName)? @"不限": self.conditionModel.filterModel.carEnvironName;
        NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:self.brightConfigSelectedArray.count];
        for (NRWBrightConfigItemModel *model in self.brightConfigSelectedArray) {
            [arrM addObject:model.confName];
        }
        self.sectionHeaderArray[7].detail = isEmptyString([arrM componentsJoinedByString:@"、"])? @"不限": [arrM componentsJoinedByString:@"、"];

        [self.collection reloadData];
    }
}

- (void)requestFailed:(__kindof YTKBaseRequest *)request {
    
	[NRWMessageHUD showMessage:@"请求失败, 请稍后再试" forTime:1.5 withRemindId:remindFail];
}

#pragma mark - collectionDelegate && DataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 9;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 2;
    } else if (section == 1) {
        return [NRWBuyCarFilterManager getPriceModelArray].count;
    } else if (section == 2 || section == 3) {
        return 1;
    } else if (section == 4) {
        return 2;
    } else if (section == 5) {
        return self.optionsModel.baseCarOutColorOptions.count;
    } else if (section == 6) {
        return self.optionsModel.baseCarTypeOptions.count;
    } else if (section == 7) {
        return self.optionsModel.baseCarEnvironOptions.count;
    } else {
        return self.configModel.result.count;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        NRWTabelViewCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"tableCell" forIndexPath:indexPath];
        
        if (indexPath.row == 0) {
            cell.titleString = @"城市";
            cell.detailString = [NSString stringWithFormat:@"%@", isEmptyString(self.conditionModel.cityModel.cityName)? @"": self.conditionModel.cityModel.cityName];
        } else {
            cell.titleString = @"品牌";
            cell.detailString = [NSString stringWithFormat:@"%@", isEmptyString(self.conditionModel.brandModel.seriesName)? @"": self.conditionModel.brandModel.seriesName];
        }
        return cell;
    } else if (indexPath.section == 1) {
        
        NRWPriceChooseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"other" forIndexPath:indexPath];
        
        cell.priceTitle = self.priceModelArray[indexPath.item].priceTitle;
        cell.isSelect = [self.conditionModel.priceModel.priceTitle isEqualToString:cell.priceTitle];
        
        return cell;
    } else if (indexPath.section == 2) {
        
        NRWSliderCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"slider" forIndexPath:indexPath];
        cell.backgroundColor = cell.contentView.backgroundColor = [UIColor whiteColor];
        [cell setTitleArr:self.carAgeTitleArray firstSelectIndex:self.conditionModel.filterModel.carAgeFirstIndex lastSelectIndex:self.conditionModel.filterModel.carAgeLastIndex];
        cell.delegate = self;
        
        return cell;
    } else if (indexPath.section == 3) {
        
        NRWSliderCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"slider" forIndexPath:indexPath];
        cell.backgroundColor = cell.contentView.backgroundColor = [UIColor whiteColor];
        [cell setTitleArr:self.carMileageTitleArray firstSelectIndex:self.conditionModel.filterModel.mileageFirstIndex lastSelectIndex:self.conditionModel.filterModel.mileageLastIndex];
        cell.delegate = self;
        
        return cell;
    } else if (indexPath.section == 4) {
        
        NRWPriceChooseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"other" forIndexPath:indexPath];
        if (indexPath.row == 0) {
            cell.priceTitle = @"手动";
        } else {
            cell.priceTitle = @"自动";
        }
        cell.isSelect = [cell.priceTitle isEqualToString:self.conditionModel.gearbox];
        return cell;
    } else if (indexPath.section == 5) {
        
        NRWFilterColorCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"color" forIndexPath:indexPath];
        cell.colorName = self.optionsModel.baseCarOutColorOptions[indexPath.item].colorName;
        cell.colorCode = self.optionsModel.baseCarOutColorOptions[indexPath.item].colorCode;
        cell.isSelect = [cell.colorName isEqualToString:self.conditionModel.filterModel.carOutColorName];
        cell.backgroundColor = cell.contentView.backgroundColor = [UIColor whiteColor];
        cell.opaque = cell.contentView.opaque = YES;
        return cell;
    } else if (indexPath.section == 6) {// 车身类型
        
        NRWPriceChooseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"other" forIndexPath:indexPath];
        cell.priceTitle = self.optionsModel.baseCarTypeOptions[indexPath.item].carTypeName;
        cell.isSelect = [self.optionsModel.baseCarTypeOptions[indexPath.item].carTypeId isEqualToString:self.conditionModel.filterModel.carTypeId];
        return cell;
    } else if (indexPath.section == 7) {// 环保类型
        
        NRWPriceChooseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"other" forIndexPath:indexPath];
        cell.priceTitle = self.optionsModel.baseCarEnvironOptions[indexPath.item];
        cell.isSelect = [cell.priceTitle isEqualToString:self.conditionModel.filterModel.carEnvironName];
        return cell;
    } else {
        NRWPriceChooseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"other" forIndexPath:indexPath];
        
        cell.priceTitle = self.configModel.result[indexPath.item].confName;
        cell.isSelect = [self.brightConfigSelectedArray containsObject:self.configModel.result[indexPath.item]];
        
        return cell;
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return nil;
    }
    
    if (kind == UICollectionElementKindSectionHeader) {
        
        NRWBuyCarFilterHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        
        header.titleStr = self.sectionHeaderArray[indexPath.section - 1].title;
        header.detailStr = self.sectionHeaderArray[indexPath.section - 1].detail;
        
        header.backgroundColor = getColor(@"#F9F9F9");
        
        return header;
    }
    
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 2 || indexPath.section == 3) {
        return;
    }
    
    if (indexPath.section == 0) {
        
        if (indexPath.item == 0) {
            
            if (self.cityPickView.isShow) {
                [self.cityPickView disMiss];
            } else {
                [self.cityPickView show];
				self.cityPickView.selectProvinceIndex = self.conditionModel.cityModel.provinceIndex;
				self.cityPickView.selectCityIndex = self.conditionModel.cityModel.cityIndex;
            }
        } else if (indexPath.item == 1) {
            
            NRWBrandSeriesVC *vc = [[NRWBrandSeriesVC alloc] init];
            vc.delegate = self;
            vc.selectBrandModel = self.conditionModel.brandModel;
            [self.navigationController pushViewController:vc animated:YES];
        }
    } else if (indexPath.section == 1) {
        
        if ([self.conditionModel.priceModel.priceTitle isEqualToString:self.priceModelArray[indexPath.item].priceTitle]) {
            return;
        }
        
        if (indexPath.item == 0) {
            
            self.conditionModel.priceModel.priceTitle = nil;
            self.conditionModel.priceModel.priceFrom = nil;
            self.conditionModel.priceModel.priceTo = nil;
            self.sectionHeaderArray[indexPath.section - 1].detail = @"不限";
        } else {
            
            self.conditionModel.priceModel.priceTitle = self.priceModelArray[indexPath.item].priceTitle;
            self.conditionModel.priceModel.priceFrom = self.priceModelArray[indexPath.item].priceFrom;
            self.conditionModel.priceModel.priceTo = self.priceModelArray[indexPath.item].priceTo;
            
            self.sectionHeaderArray[indexPath.section - 1].detail = self.conditionModel.priceModel.priceTitle;
        }
        
    } else if (indexPath.section == 4) {
        
        if (indexPath.item == 0) {
            self.conditionModel.gearbox = @"手动";
        }  else {
            self.conditionModel.gearbox = @"自动";
        }
        self.sectionHeaderArray[indexPath.section - 1].detail = self.conditionModel.gearbox;
    } else if (indexPath.section == 5) {
        
        self.conditionModel.filterModel.carOutColorCode = self.optionsModel.baseCarOutColorOptions[indexPath.item].colorCode;
        self.conditionModel.filterModel.carOutColorName = self.optionsModel.baseCarOutColorOptions[indexPath.item].colorName;
        self.sectionHeaderArray[indexPath.section - 1].detail = self.conditionModel.filterModel.carOutColorName;
    } else if (indexPath.section == 6) {
        
        self.conditionModel.filterModel.carTypeId = self.optionsModel.baseCarTypeOptions[indexPath.item].carTypeId;
        self.conditionModel.filterModel.carTypeName = self.optionsModel.baseCarTypeOptions[indexPath.item].carTypeName;
        self.sectionHeaderArray[indexPath.section - 1].detail = self.optionsModel.baseCarTypeOptions[indexPath.item].carTypeName;
        
    } else if (indexPath.section == 7) {
        
        self.conditionModel.filterModel.carEnvironName = self.optionsModel.baseCarEnvironOptions[indexPath.item];
        self.sectionHeaderArray[indexPath.section - 1].detail = self.optionsModel.baseCarEnvironOptions[indexPath.item];
    } else if (indexPath.section == 8) {
        
        if ([self.brightConfigSelectedArray containsObject:self.configModel.result[indexPath.item]]) {
            
            [self.brightConfigSelectedArray removeObject:self.configModel.result[indexPath.item]];
        } else {
            [self.brightConfigSelectedArray addObject:self.configModel.result[indexPath.item]];
        }
        
        NSMutableArray *arrM = [NSMutableArray array];
        for (NRWBrightConfigItemModel *model in self.brightConfigSelectedArray) {
            [arrM addObject:model.confName];
        }
        self.sectionHeaderArray[indexPath.section - 1].detail = [arrM componentsJoinedByString:@"、"];
    }
    
    [UIView animateWithDuration:0 animations:^{
        [self.collection performBatchUpdates:^{
            [self.collection reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]];
        } completion:nil];
    }];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return CGSizeZero;
    }
    
    return CGSizeMake(SCREEN_WIDTH(), 30);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return CGSizeMake(SCREEN_WIDTH(), 45);
    } else if (indexPath.section == 1 || indexPath.section == 4 || indexPath.section > 5) {
        return CGSizeMake(75, 30);
    } else if (indexPath.section == 2 || indexPath.section == 3) {
        return CGSizeMake(SCREEN_WIDTH(), 90);
    } else {
        return CGSizeMake(40 , 55);
    }
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {

    if (section == 2 || section == 3 || section == 0) {
        return 0;
    }
    return 20;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    
    if (SCREEN_WIDTH() == 320) {
        if (section == 5) {
            return (int)((SCREEN_WIDTH() - 262) / 4);
        }
        return 0;
    }

    if (section == 5) {
        return (int)((SCREEN_WIDTH() - 262) / 4);
    } else if (section != 2 || section != 3) {
        return (int)((SCREEN_WIDTH() - 32 - 75 * 4) / 3);
    }
    return 0;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {

    if (section == 5) {
        return UIEdgeInsetsMake(10, 31, 10, 31);
    } else if (section == 1 || section == 4 || section > 5) {
        return UIEdgeInsetsMake(10, 16, 10, 16);
    }
    return UIEdgeInsetsZero;
}

#pragma mark - NRWCooperateCityPickViewDelegate
- (void)cooperateCityPickView:(NRWCooperateCityPickView *)view confirmButtonDidClick:(UIButton *)sender withProvinceName:(NSString *)provinceName provinceId:(NSString *)provinceId cityName:(NSString *)cityName cityId:(NSString *)cityId provinceIndex:(NSInteger)provinceIndex cityIndex:(NSInteger)cityIndex {
    
    [view disMiss];
    
    if ([self.conditionModel.cityModel.cityId isEqualToString:cityId]) {
        return;
    }
    
    self.conditionModel.cityModel.provinceName = provinceName;
    self.conditionModel.cityModel.provinceId = provinceId;
    self.conditionModel.cityModel.cityName = cityName;
    self.conditionModel.cityModel.cityId = cityId;
	self.conditionModel.cityModel.provinceIndex = provinceIndex;
	self.conditionModel.cityModel.cityIndex = cityIndex;
    
    [UIView animateWithDuration:0 animations:^{
        [self.collection performBatchUpdates:^{
            [self.collection reloadSections:[NSIndexSet indexSetWithIndex:0]];
        } completion:nil];
    }];
}

#pragma mark - NRWBrandSeriesVCDelegate
- (void)brandSeriesVC:(NRWBrandSeriesVC *)view selectBrandId:(NSString *)selectBrandId selectBrandName:(NSString *)selectBrandName selectSeriesId:(NSString *)selectSeriesId selectSeriesName:(NSString *)selectSeriesName {
    
    if ([selectBrandId isEqualToString:self.conditionModel.brandModel.brandId] && [selectSeriesId isEqualToString:self.conditionModel.brandModel.seriesId]) {
        return;
    }
    
    self.conditionModel.brandModel.brandName = selectBrandName;
    self.conditionModel.brandModel.brandId = selectBrandId;
    self.conditionModel.brandModel.seriesName = selectSeriesName;
    self.conditionModel.brandModel.seriesId = selectSeriesId;
    
    [UIView animateWithDuration:0 animations:^{
        [self.collection performBatchUpdates:^{
            [self.collection reloadSections:[NSIndexSet indexSetWithIndex:0]];
        } completion:nil];
    }];
}

#pragma mark - NRWSliderCollectionCellDelegate
- (void)sliderCollectionCell:(NRWSliderCollectionCell *)cell withFirstIndexPath:(int)firstIndexPath lastIndexPath:(int)lastIndexPath {
    
    NSInteger section = 0;
    
    if ([self.collection indexPathForCell:cell].section == 2) {//车龄
        
        section = 2;
        
        [NRWBuyCarFilterManager setValueForModel:self.conditionModel.filterModel headerModel:self.sectionHeaderArray[section - 1] withTitleModel:self.carAgeTitleArray firstIndex:firstIndexPath lastIndex:lastIndexPath type:NRWBuyCarFilterTypeCarAge];
    } else if ([self.collection indexPathForCell:cell].section == 3) {
        
        section = 3;
        
        [NRWBuyCarFilterManager setValueForModel:self.conditionModel.filterModel headerModel:self.sectionHeaderArray[section - 1] withTitleModel:self.carMileageTitleArray firstIndex:firstIndexPath lastIndex:lastIndexPath type:NRWBuyCarFilterTypeMileage];
    }
    
    [UIView animateWithDuration:0 animations:^{
        [self.collection performBatchUpdates:^{
            [self.collection reloadSections:[NSIndexSet indexSetWithIndex:section]];
        } completion:nil];
    }];
}

#pragma mark - getter
- (NSArray<NSString *> *)carMileageTitleArray {
    if (_carMileageTitleArray == nil) {
        _carMileageTitleArray = [NRWBuyCarFilterManager getBuyCarMileageTitleArray];
    }
    return _carMileageTitleArray;
}

- (NSArray<NSString *> *)carAgeTitleArray {
    if (_carAgeTitleArray == nil) {
        _carAgeTitleArray = [NRWBuyCarFilterManager getBuyCarAgeTitleArray];
    }
    return _carAgeTitleArray;
}

- (NSArray<NRWBuyCarPriceModel *> *)priceModelArray {
    if (_priceModelArray == nil) {
        _priceModelArray = [NRWBuyCarFilterManager getPriceModelArray];
    }
    return _priceModelArray;
}

- (NSArray<NRWBuyCarFilterHeaderModel *> *)sectionHeaderArray {
    
    if (_sectionHeaderArray == nil) {
        _sectionHeaderArray = [NRWBuyCarFilterManager getSubscribeTitleArray];
    }
    return _sectionHeaderArray;
}

- (NRWBuyCarConditionModel *)conditionModel {
    
    if (_conditionModel == nil) {
        _conditionModel = [[NRWBuyCarConditionModel alloc] init];
    }
    return _conditionModel;
}

- (NRWGetFilterInfoRequest *)infoApi {
    
    if (_infoApi == nil) {
        _infoApi = [NRWGetFilterInfoRequest requestWithDelegate:self];
    }
    return _infoApi;
}

- (NRWGetBrightSpotConfListAPI *)brightConfigApi {
    
    if (_brightConfigApi == nil) {
        _brightConfigApi = [NRWGetBrightSpotConfListAPI requestWithDelegate:self];
    }
    return _brightConfigApi;
}

- (NRWGetCooperationPositionApi *)positionApi {
    
    if (_positionApi == nil) {
        _positionApi = [NRWGetCooperationPositionApi requestWithDelegate:self];
    }
    return _positionApi;
}

- (NRWCooperateCityPickView *)cityPickView {
    
    if (_cityPickView == nil) {
        _cityPickView = [[NRWCooperateCityPickView alloc] init];
        _cityPickView.delegate = self;
    }
    return _cityPickView;
}

- (NSMutableArray<NRWBrightConfigItemModel *> *)brightConfigSelectedArray {
    
    if (_brightConfigSelectedArray == nil) {
        _brightConfigSelectedArray = [NSMutableArray array];
    }
    return _brightConfigSelectedArray;
}

- (NRWInsertSubscribeInfoApi *)insertSubscribeInfoApi {
    
    if (_insertSubscribeInfoApi == nil) {
        _insertSubscribeInfoApi = [NRWInsertSubscribeInfoApi requestWithDelegate:self];
    }
    return _insertSubscribeInfoApi;
}

- (NRWSearchSubcribeApi *)searchSubcribeApi {
    
    if (_searchSubcribeApi == nil) {
        _searchSubcribeApi = [NRWSearchSubcribeApi requestWithDelegate:self];
        _searchSubcribeApi.userId = [NRWUserInfoModel shared].userId;
    }
    return _searchSubcribeApi;
}

@end
